package leetcode.twopoint;

import java.util.HashMap;



public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        String s = "abcabcbb";
        s = "aab";
        int i = lengthOfLongestSubstring(s);
        System.out.println(i);
    }

    public static int lengthOfLongestSubstring(String s) {
        int len = s.length();
        if(len==0){
            return 0;
        }
        int start = 0;
        int end = 0;
        int maxLen = 1;
        HashMap<Character, Integer> map = new HashMap<>();
        while (end < len) {
            Integer tmp = map.get(s.charAt(end));
            if (tmp == null || tmp < start) {
                maxLen = Math.max(maxLen, end - start + 1);
            }else {
                start = tmp + 1;
            }
            map.put(s.charAt(end), end);
            end++;
        }
        return maxLen;
    }
}
